
/**
     * 格式化趋势图横坐标日期
     */
function xAxisDateFormat(categories, tmpDate, dateType){
   	if(dateType== '02' || dateType== '03'){
   		categories.push(tmpDate.substring(5));
   	}else if(dateType== '04' || dateType== '05'){
   		categories.push(tmpDate.substring(8)+"日");
   	}else if(dateType== '06' || dateType== '07'){
   		categories.push(tmpDate.substring(5)+"月");
   	}
}

/**
 * 格式化安全趋势数据
 */
var formatSafetyTendencyData = function(data, dateType, category, series, dateAr){
    if (data && data.length > 0) {
        report_date = data[0].reportDate;
        for (var i = 0; i < data.length; i++) {
            if (report_date != data[i].reportDate) {
                break;
            }
            var safeTrendItem = data[i].safeTrendItem? data[i].safeTrendItem : "未知";
            var element = {name: safeTrendItem, data: []};
            series.push(element);
        }
        var step = series.length;
        for (var i = 0; i < data.length; i += step) {
        	xAxisDateFormat(category, data[i].reportDate, dateType);
            for (var s = 0; s < step; s++) {
            	var val = parseFloat(data[i+s].safeTrendVal);
				if(!val){
					val = 0;
				}
                series[s].data.push(val);
            }
        }
    }else{
    	//TODO
    	mainTitle = '暂无数据';
    	subTitle = '线路：- 时间：-';
		series.push({name:"暂无数据", data:[]});
		for(var i=0;i<dateAr.length;i++){
			xAxisDateFormat(category, dateAr[i], dateType);
			series[0].data.push(0);
		}
    }
}

/**
 * 生成多维趋势图（能耗成本分析，故障趋势，不安全行为趋势）
 */
var createMultiTendencyChart = function(obj, mainTitle, subTitle, category, colors, unit, series, dateType){
	if(colors){
		Highcharts.setOptions({  
	       colors: colors
		}); 
	}
	var interval = 1;
	if(dateType == '04' || dateType == '05'){
		interval = 2;
	}
	obj.highcharts({
    	title: {
    		text: mainTitle,
    		x: -20
    	},
    	subtitle: { 
			text: subTitle
		},
    	chart: { 
    		type: 'spline' 
    	},
    	credits: {
    		enabled: false
    	},
    	xAxis: {
    		tickInterval: interval,
    		categories: category
    		},
    	yAxis: {
    		title: {text: '单位：'+unit},
    		plotLines: [{value: 0, width: 1, color: '#808080'}],
    		labels : {
                 formatter : function ( ) {
                     return this . value ;
                 }
             }
    	},
    	tooltip: {valueSuffix: unit},
    	legend: {
    		align: 'center', 
			verticalAlign: 'bottom',  
			layout: 'horizontal',
    		borderWidth: 0
    	},
    	series: series
    });
}


/**
 * 格式化能耗趋势数据
 */
var formatEnergyTendencyData = function(data, dateType, category, series, dateAr){
	if(data && data.length>0){
		for(var i=0;i<data.length;i++){
			xAxisDateFormat(category, data[i].reportDate, dateType);
			var val = parseFloat(data[i].energyTrendVal);
			if(!val){
				val = 0;
			}
			series[0].data.push(val);
		}
	}else{
		for(var i=0;i<dateAr.length;i++){
			xAxisDateFormat(category, dateAr[i], dateType);
			series[0].data.push(0);
		}
	}
}

/**
 * 能耗趋势图
 */
var createEnergyTendencyChart = function(obj, type, color, maintitle, subTtile, category, unit, series, dateType, idxType, data, lineName, dateString){
	var interval = 1;
	if(dateType=='04' || dateType=='05'){
		interval = 2;
	}
	if(idxType == '030801'){
		series[0].name = '运行里程';
		mainTitle = '';
		subTitle = '线路：'+lineName+' 时间：'+dateString;
	}
	if(!data || data.length<=0){
		mainTitle = '暂无数据';
		subTitle = '线路：- 时间：-';
	}
	obj.highcharts({
		chart: {
			type: type
		},
		colors:[color],
		title: {
			text: maintitle
		},
		subtitle: { 
			text: subTtile
		},
		credits: {
			enabled: false
		},
		xAxis: {
			tickInterval: interval,
			categories: category
		},
		yAxis: {
			title: {
				text: '单位：'+unit
			},
			labels : {
                 formatter : function ( ) {
                     return this . value ;
                 }
             }
		},
		tooltip: {
			valueSuffix: unit
		},
		series:series
	});
}

/**
 * 保存选择的查询条件（线路，日期）
 */
var persistCurrentCondition = function(lineId, lineName, dateType){
	$.setCurrentLine(lineId, lineName);
	$.setDateType(dateType);
}

/**
 * 获取当前查询条件（线路，日期）
 */
var getCurrentCondition = function(){
	var condition = [];
	condition.push(window.parent.$.cookie('homepage_lineId'));
	condition.push(window.parent.$.cookie('homepage_dateType'));
	return condition;
};


	
	
	
